package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSONObject;
import com.example.demo.common.ResultDTO;
import com.example.demo.utils.importUtils.ImportDTO;
import com.example.demo.utils.importUtils.ImportListener;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 导入controller
 *
 * @author xiaobeibei@gogpay.cn
 * @date 2023-12-08 11:34
 */
@RestController
@RequestMapping("/import")
public class ImportController {


    @PostMapping("/data")
    public ResultDTO importData(@RequestParam(value = "file") MultipartFile file) {
        if (file == null) {
            return new ResultDTO(500, "待上传的文件不能为空！");
        }
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultDTO(500, "待上传的文件格式异常！");
        }
        // 完善参数
        JSONObject form = new JSONObject();
        ImportListener importListener = new ImportListener(form);
        EasyExcel.read(inputStream, ImportDTO.class, importListener).sheet().doRead();
        return new ResultDTO(200, "导入成功");
    }

    public static void main(String[] args) {
        String s="建筑结构构件外观质量与缺陷检查； ";
        String s1="围护系统承重查";
        int indexOf = s.indexOf("结构");
        System.out.println(indexOf);
        System.out.println(s.substring(indexOf+2,indexOf+5));

    }
}
